﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Show_LED_New
{
    public partial class MainView : Form
    {
        // 用来传递信息的静态变量 
        // 每当pagename发生变化的时候，画面就有一个切换
        private static string pagename = "";
        public static bool standby_is;

        // 命令控制
        private static bool isreccive = false;
        private static string cmd = "";

        // 初始化所有的窗口
        public static StandBy standbyview = new StandBy();
        public static StartView startview = new StartView();
        public static XinxiChaxun xinxiChaxun = new XinxiChaxun();

        // 转换场景函数
       public void ReSet_View(Form myform)
       {
           foreach(Control mycontrol in panel1.Controls)
           {
               panel1.Controls.Remove(mycontrol);
           }
           myform.TopLevel = false;
           panel1.Controls.Add(myform);
           myform.Show();
           myform.Top = 0;
           myform.Left = 0;
           myform.Width = panel1.Width;
           myform.Height = panel1.Height;
           // 确定是否在待机状态下
           if(myform.Name=="StandBy")
           {
               standby_is = true;
           }
           else
           {
               standby_is = false;
           }
       }

        // 发送命令函数
        public static void Make_cmd(string mycmd)
        {
            cmd = mycmd;
            isreccive = true;
        }

        private TcpListener listener;
        public MainView()
        {
            InitializeComponent();
            // 开启TCPlistener
            IPAddress ip = IPAddress.Parse("192.168.0.116");
            listener = new TcpListener(ip,1000);
            listener.Start();
            Thread mythread = new Thread(listening);
            mythread.Start();

            Rectangle rect=System.Windows.Forms.SystemInformation.VirtualScreen;
            this.Top = 0;
            this.Left = 0;
            this.Width = rect.Width;
            this.Height = rect.Height;

            panel1.Top = 0;
            panel1.Left = 0;
            panel1.Width = this.Width;
            panel1.Height = this.Height;

            ReSet_View(standbyview);
            
        }

        
         

        private void MainView_Load(object sender, EventArgs e)
        {
            startview.Set_Button();
        }

        private void listening()
        {
            while(true)
            {
                TcpClient client = listener.AcceptTcpClient();
                RemoteClient wapper = new RemoteClient(client);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(isreccive==true)
            {
                isreccive = false;
                if(standby_is==true)
                {
                    ReSet_View(startview);
                }
                else
                {
                    Control myview = panel1.Controls[0];
                    SubView subview = (SubView)myview;
                    subview.Receive(cmd);   // 接信息传送给图像
                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
